#include "wifi_init.h"
#include "bleconfig.h"


void WIFI_connect(void)
{
    if(!WiFi.isConnected())
    {
    while(WiFi.waitForConnectResult()!=WL_CONNECTED)
    {
        WiFi.reconnect();
        Serial.println("reconnecting..");
        delay(5000);
    }
    Serial.printf("\nWiFi connected ");
    Serial.printf("IP address: ");
    Serial.println(WiFi.localIP());
    Serial.printf("MAC:%s\n",mac);
    }
}



void WIFI_init(void)
{
    // char ho
    strcpy(host_name+strlen(host_name),mac);
    WiFi.setHostname(host_name);

    // 开始扫描WiFi网络
    Serial.println("Scanning for WiFi networks...");
    int numNetworks = WiFi.scanNetworks();
    if (numNetworks == 0) {
    Serial.println("No networks found");
    esp_restart();
    } 
    else {
    Serial.println("Networks found:");
    int maxSignalStrength = -100; // 初始信号强度设置为最低
    int bestNetworkIndex = -1;

    // 遍历所有扫描到的网络
    for (int i = 0; i < numNetworks; i++) {

        if (WiFi.SSID(i) == SSID.c_str()) {
        if(WiFi.RSSI(i) > maxSignalStrength){
        maxSignalStrength = WiFi.RSSI(i);
        bestNetworkIndex = i;
        }
        Serial.print("name: "); // 打印网络名称
        Serial.println(WiFi.SSID(i)); // 打印SSID
        Serial.print("RSSI: "); // 打印信号强度
        Serial.println(WiFi.RSSI(i)); // 打印信号强度值
        Serial.print("MAC: "); // 打印MAC地址
        Serial.println(WiFi.BSSIDstr(i)); // 打印MAC地址字符串形式
        Serial.println("-----------------------");
        }
    }

    // 如果找到信号最强的目标SSID
    if (bestNetworkIndex != -1) {
        Serial.print("Connecting to network: ");
        Serial.println(WiFi.SSID(bestNetworkIndex));
        WiFi.begin(SSID.c_str(), PASSWD.c_str(), 0, WiFi.BSSID(bestNetworkIndex));
        WiFi.setAutoConnect(true);
        WiFi.setAutoReconnect(true);
        // 等待连接成功
        uint32_t start_times,now_times;
        start_times=millis();
        while (WiFi.status() != WL_CONNECTED) {
        now_times=millis();
        if(now_times-start_times>=20000){
        WiFi.disconnect();
        Serial.printf("start bleconfig\n");
        xTaskCreatePinnedToCore(bleconfig,    //蓝牙配网进程，在wifi连接成功后会自删除
                            "bleconfig",  //任务名称
                            0x1000,       //堆栈大小
                            NULL,       //输入参数
                            1,          //任务优先级
                            NULL,       //
                            0           //核心  0\1
                            );//
        for(;;){
            if(WiFi.status() == WL_CONNECTED){
                esp_restart();
            }
            delay(2000);
        }
        }
        delay(500);
        Serial.print(".");
        }

        Serial.println("");
        Serial.println("Connected to WiFi");
        Serial.print("IP address: ");
        Serial.println(WiFi.localIP());
    } else {
        Serial.println("Target SSID not found or signal is too weak");
        Serial.printf("start bleconfig\n");
        xTaskCreatePinnedToCore(bleconfig,    //蓝牙配网进程，在wifi连接成功后会自删除
                            "bleconfig",  //任务名称
                            0x1000,       //堆栈大小
                            NULL,       //输入参数
                            1,          //任务优先级
                            NULL,       //
                            0           //核心  0\1
                            );//
        for(;;){
            if(WiFi.status() == WL_CONNECTED){
                esp_restart();
            }
            delay(2000);
        }
        
    }
    }

    

    // WIFI_connect();
}